package com.health.mapp.impl;

import io.rong.models.TokenReslut;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.health.commons.PropertiesService;
import com.health.db.dto.StuffDetailDto;
import com.health.db.model.UserLIST;
import com.health.db.service.sm.IRongService;
import com.health.db.service.user.IStuffListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.GetTokenReq;
import com.health.mapp.bean.GetTokenResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;


public class GetTokenImpl implements ActionInterface{
	
	private static Logger logger = Logger.getLogger(GetTokenImpl.class);

	private IUserLISTService userLISTService  ;
	private IRongService rongService  ;
	private IStuffListService stuffListService;

	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		// TODO Auto-generated method stub
		GetTokenReq req=(GetTokenReq)reqBean;
		GetTokenResp _resp=new GetTokenResp();
		UserLIST userLIST=userLISTService.selectByUserId(Long.valueOf(req.getUserId()));
		if(null!=userLIST)
		{
			logger.info("UserId:"+userLIST.getUserid());
			logger.info("UserName:"+userLIST.getUserName());
			
			String iconUrl = userLIST.getIconUrl();
			//如果是医生
			if (userLIST.getTypeid().intValue()==Constant.CONSTANTS_USER_ROLE_ID_3.intValue()) {
				//docImage/kefu.png
				 StuffDetailDto stuffDetailDto = stuffListService.selectByStuffId(Long.valueOf(req.getUserId()));
				
				 
				 if (null!=stuffDetailDto) {
					 String docIconUrl=stuffDetailDto.getIconUrl();
						if (StringUtils.isNotBlank(docIconUrl)) {
							iconUrl =docIconUrl;
						}else{
							iconUrl = PropertiesService.getString("iconUrl.docDefaultUrl").trim();
						}
				}else{
					_resp.setErrorCode(ResultCode.LOGIN_FAIL_MEMBER_IS_NOT_EXISTS);
					_resp.setErrorMsg(ResultCode.LOGIN_FAIL_MEMBER_IS_NOT_EXISTS_NOTE);
					return _resp;
				}
			}else{
				if (StringUtils.isBlank(iconUrl)) {
					iconUrl = PropertiesService.getString("iconUrl.userDefaultUrl").trim();
				}
			}
			
			// 获取 Token 方法 
			TokenReslut userGetTokenResult =rongService.getUserToken(String.valueOf(userLIST.getUserid()), userLIST.getUserName(),iconUrl);
			if(userGetTokenResult.getCode().intValue()==200)
			{
				userLIST.setApptoken(userGetTokenResult.getToken());
				userLISTService.updateByPrimaryKey(userLIST);
				_resp.setErrorCode(0);
				_resp.setErrorMsg("Success");
				_resp.setUserId(userGetTokenResult.getUserId());
				_resp.setToken(userGetTokenResult.getToken());
			}else{
				_resp.setErrorCode(-1);
				_resp.setErrorMsg(userGetTokenResult.getErrorMessage());
			}
		}else
		{
			_resp.setErrorCode(-1);
			_resp.setErrorMsg("no this people");
		}

		return _resp;
	}


	 

	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}




	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}




	public IRongService getRongService() {
		return rongService;
	}


	public void setRongService(IRongService rongService) {
		this.rongService = rongService;
	}




	/**
	 * @return the stuffListService
	 */
	public IStuffListService getStuffListService() {
		return stuffListService;
	}




	/**
	 * @param stuffListService the stuffListService to set
	 */
	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}

	
	
}
